﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;

namespace MedicalDB
{
    class Validation
    {

        private string controale;
        private Control cont;
        private string value;
        private string eroare;

        public Validation(Control contrl, string value)
        {
            this.cont = contrl;
            this.controale = contrl.Name;
            this.value = value;
        }

        public string getError()
        {
            return eroare;
        }

        public bool validate()
        {
            string xmlFile = @"C:\Validate.xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> Dictvalidation = xmlParser.ExtractAttributePairs(controale, string.Empty, string.Empty, "name", "formula");

            eroare = "length_min";
            if (!Dictvalidation["length_min"].Equals("N/A"))
                if (value.Length < Convert.ToInt32(Dictvalidation["length_min"]))
                {
                    changeColor();
                    return false;
                }

            eroare = "length_max";
            if (!Dictvalidation["length_max"].Equals("N/A"))
                if (value.Length > Convert.ToInt32(Dictvalidation["length_max"]))
                {
                    changeColor();
                    return false;
                }
            
            eroare = "length";
            if (!Dictvalidation["length"].Equals("N/A"))
                if (value.Length != Convert.ToInt32(Dictvalidation["length"]))
                {
                    changeColor();
                    return false;
                }

            eroare = "type";
            if (!Dictvalidation["type"].Equals("N/A"))
            {
                if (Dictvalidation["type"].Equals("0"))
                {
                    if (!IsNumber(value))
                    {
                        changeColor();
                        return false;
                    }
                }
                else
                {
                    if (Dictvalidation["type"].Equals("1"))
                    {
                        if (!IsAlpha(value))
                        {
                            changeColor();
                            return false;
                        }
                    }
                    else
                        if (!IsAlphaNumeric(value))
                        {
                            changeColor();
                            return true;
                        }
                }
            }

            eroare="max";
            if (!Dictvalidation["max"].Equals("N/A"))
            {
                if (Dictvalidation["type"].Equals("3"))
                {   
                    DateTime max;
                    if(Dictvalidation["max"].Equals("currentTime"))
                        max = DateTime.Now;
                    else
                        max = DateTime.Parse(Dictvalidation["max"]);
                    int a = max.CompareTo(DateTime.Parse(value)); 
                    if (max.CompareTo(DateTime.Parse(value)) == -1)
                    {
                        changeColor();
                        return false;
                    }
                }
                else
                    if (value.Equals(Convert.ToInt32(Dictvalidation["max"])))
                    {
                        changeColor();
                        return false;
                    }
            }

            eroare="min";
            if (!Dictvalidation["min"].Equals("N/A"))
                if (Dictvalidation["type"].Equals("3"))
                {
                    DateTime min;
                    if (Dictvalidation["min"].Equals("currentTime"))
                        min = DateTime.Now;
                    else
                        min = DateTime.Parse(Dictvalidation["min"]);
                    if (min.CompareTo(DateTime.Parse(value)) == 1)
                    {
                        changeColor();
                        return false;
                    }
                }
                else
                    if (value.Equals(Convert.ToInt32(Dictvalidation["min"])))
                    {
                        changeColor();
                        return false;
                    }

            eroare="values";
            string[] valoriPosibile = Dictvalidation["values"].Split(';');
            if (!Dictvalidation["values"].Equals("N/A"))
                if (!valoriPosibile.Contains(value))
                {
                    changeColor();
                    return false;
                }

            return true;
        }

        private void changeColor()
        {
            if (cont.GetType() == typeof(TextBox))
            {
                cont.BackColor = Color.Red;
            }
            if (cont.GetType() == typeof(DateTimePicker))
            {
                cont.BackColor = Color.Red;
            }
            if (cont.GetType() == typeof(ComboBox))
            {
                cont.BackColor = Color.Red;
            }
        }

        // Function to test whether the string is valid number or not
        private bool IsNumber(String strNumber)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
            return !objNotNumberPattern.IsMatch(strNumber) &&
            !objTwoDotPattern.IsMatch(strNumber) &&
            !objTwoMinusPattern.IsMatch(strNumber) &&
            objNumberPattern.IsMatch(strNumber);
        }
        // Function To test for Alphabets.
        private bool IsAlpha(String strToCheck)
        {
            Regex objAlphaPattern = new Regex("[^a-zA-Z]");
            return !objAlphaPattern.IsMatch(strToCheck);
        }


        // Function to Check for AlphaNumeric.
        private bool IsAlphaNumeric(String strToCheck)
        {
            Regex objAlphaNumericPattern = new Regex("[a-zA-Z0-9]");
            //return !objAlphaNumericPattern.IsMatch(strToCheck);
            return true;
        }
    }
}
